const express=require("express");
const bodyParser=require("body-parser");
var app=express();

// app.get("/books",function(req,res){
// 	res.send("all books");
// });
// app.post("/books",function(req,res){
// 	res.send("add a book");
// })

var books=[
	{id:1,name:"a1",price:23.45},
	{id:2,name:"a2",price:45.67}
]
// app.route("/books")
// .get(function(req,res){

// 	res.json(books);
// })
// .post(function(req,res){
// 	res.send("add a book");
// });
var router=express.Router();
router.route("/books")
.get(function(req,res){
	res.json(books);
})
.post(function(req,res){
	books.push(req.body);
	res.json(req.body);
});
router.route("/books/:id")
.get(function(req,res){
	var id=req.params.id;
	var book=books.filter(b=>{
		return b.id==id;
	});
	res.json(book);

})
.delete((req,res)=>{
	var id=req.params.id;
	books=books.filter(b=>{return b.id!=id});
	res.json({});
})


app.use("/",bodyParser.json());
app.use("/",router);






app.listen(6008,function(err){
	if(!err){
		console.log("express start in 6000")
	}
});